﻿
package ui {
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	public class HBox extends UIContainer {
		private static const MARGIN:Number = 5;
		private static const SPACE:Number = 3;
		
		public var background:Boolean = false;
		
		private var align:String;
		
		public function HBox(options:Object = null) {
			marginTop = marginLeft = 0;
			applyOptions(options);
			this.align = align;
			update();
		}
		
		override public function addChild(child:DisplayObject):DisplayObject {
			var d:DisplayObject = super.addChild(child);
			update();
			return d;
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject {
			var d:DisplayObject = super.removeChild(child);
			update();
			return d;
		}
		
		public function update():void {
			var i:int;
			var d:DisplayObject;
			
			w = 0;
			h = 0;
			for (i = 0; i < numChildren; i++) {
				d = getChildAt(i);
				//if ((background || (!background && i > 0)) && d is UIItem) {
				//if (background && d is UIItem) d.y = (d as UIItem).marginTop;
				
				if (d is UIItem) d.y = (d as UIItem).marginTop;
				//d.y += MARGIN;
				//d.y = center-d.height/2;
				if ((background	|| (!background && i > 0)) && d is UIItem) w += (d as UIItem).marginLeft;
				d.x = w;
				w += d.width+SPACE+(d is UIItem ? (d as UIItem).marginRight : 0);
				if (d.height > h) h = d.height;
			}
			
			//h += MARGIN*2;
			//w += MARGIN-SPACE;
			
			if (background) {
				var g:Graphics = graphics;
				g.clear();
				g.lineStyle(1, 0x000000, 0.1);
				g.beginFill(0xFFFFFF, 0.9);
				g.drawRoundRect(-MARGIN, -MARGIN, w+MARGIN*2, h+MARGIN*2, 6, 6);
				g.endFill();
			}
		}
	}
}